<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Message\Http\Controller\Api;

use App\Http\Admin\Controller\AbstractController as AdminAbstractController;
use App\Http\Common\Middleware\AccessTokenMiddleware;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Request;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Mine\Swagger\Attributes\PageResponse;
use Plugin\Message\Schema\MessageSchema;
use Plugin\Message\Service\MessageService;

/**
 * 消息表控制器
 * Class MessageApiController
 */
#[HyperfServer(name: 'http')]
#[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
final class MessageApiController extends AdminAbstractController
{

    /**
     * 业务处理服务
     * MessageService
     */
    public function __construct(
        protected readonly MessageService $service,
        protected readonly CurrentUser    $currentUser
    )
    {
    }


    #[Get(
        path: '/message/api/list',
        summary: '消息表控制器列表',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['消息表控制器'],
    )]
    #[PageResponse(instance: MessageSchema::class)]
    public function page(Request $request): Result
    {
        return $this->success(data: $this->service->apiPage(
            array_merge($request->all(), ['user_id' => $this->currentUser->id()]),
            (int)$request->query('page'),
            (int)$request->query('page_size')
        ));
    }

    #[Get(
        path: '/message/api/read/{id}',
        summary: '消息表控制器详情',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['消息表控制器']
    )]
    public function read(int $id): Result
    {
        return $this->success(data: $this->service->read($id, $this->currentUser->id()));
    }

    #[Get(
        path: '/message/api/unread',
        summary: '消息表控制器未读列表',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['消息表控制器']
    )]
    #[PageResponse(instance: MessageSchema::class)]
    public function unread(): Result
    {
        return $this->success(data: $this->service->apiCountUnread($this->currentUser->id()));
    }

    #[Get(
        path: '/message/api/oneKeyRead',
        summary: '消息表控制器一键读取',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['消息表控制器']
    )]
    public function oneKeyRead(): Result
    {
        return $this->success(data: $this->service->oneKeyRead($this->currentUser->id()));
    }

}